package com.lijiajian.controller;

import com.lijiajian.Service.ModuleService;
import com.lijiajian.annoation.RequiredPermission;
import com.lijiajian.base.BaseController;
import com.lijiajian.base.ResultInfo;
import com.lijiajian.model.TreeModel;
import com.lijiajian.pojo.Module;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("module")
public class ModuleController extends BaseController {
    @Resource
    private ModuleService moduleService;

    /**
     * 查询资源列表（树形目录）
     * @return
     */
    @GetMapping("list")
    @ResponseBody
    public List<TreeModel> getModuleList(Integer roleId){


        return moduleService.queryAllModules(roleId);
    }

    /**
     * 前往授权页面
     * @return
     */
    @GetMapping("toModulePage")
    public String toModulePage(Integer roleId, HttpServletRequest request){
        if(roleId!=null){
            request.setAttribute("roleId",roleId);
        }

        return "role/grant";
    }


    /**
     * 前往资源列表
     *
     */
    @RequiredPermission(code="6030")
    @GetMapping("index")
    public String toModuleList(){
        return "module/module";
    }

    /**
     * 查询资源列表（表格）
     */
    @RequiredPermission(code="603002")
    @GetMapping("moduleList")
    @ResponseBody
    public Map<String,Object> queryModuleList(){


        return moduleService.queryModuleList();
    }

    @RequiredPermission(code="603001")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addModule(Module module){
        moduleService.addModule(module);
        return success("添加成功");
    }

    @GetMapping("toAddModule")
    public String toAddModule(Integer grade,Integer parentId,HttpServletRequest request){
        if(grade!=null&&parentId!=null){
            request.setAttribute("grade",grade);
            request.setAttribute("parentId",parentId);
        }
        return "module/addModule";
    }

    /**
     * 修改资源
     * @param module
     * @return
     */
    @RequiredPermission(code="603003")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateModule(Module module){
        moduleService.updateModule(module);
        return success("修改成功");
    }

    @GetMapping("toUpdateModule")
    public String toUpdateModule(Integer id,HttpServletRequest request){
        request.setAttribute("module",moduleService.selectByPrimaryKey(id));
        return "module/updateModule";
    }

    /**
     * 删除资源
     * @param id
     * @return
     */
    @RequiredPermission(code="603004")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteModule(Integer id){
        moduleService.deleteModule(id);
        return success("删除成功");
    }
}
